package com.chlp.core.framework.config;

import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Optional;
import java.util.function.Function;

/**
 * @author 陈礼鹏
 * @date 2020/1/16 17:23
 */
@Configuration
@EnableWebMvc
@EnableSwagger2
@ConditionalOnProperty(name = "cafe.enable.interface.swagger", havingValue = "true")
public class SwaggerConfig implements WebMvcConfigurer {

	@Value("${server.servlet.context-path}")
	private String contextPath;

	@Value("${spring.application.name}")
	private String application;

	@Value("${server.port}")
	private String port;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				// 注意修改此处的包名
				.apis(basePackage("com.chlp")).paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title(application + "模块接口 v1.0")
				.description("在线接口测试")
				.termsOfServiceUrl("http://localhost:" +port+ "/" + contextPath + "/swagger-ui.html")
				.version("1.0")
				.build();
	}

	/**
	 * com.google.common.base.Predicate;
	 */
	private static Predicate<RequestHandler> basePackage(String basePackage) {
		return input -> {
			assert input != null;
			return declaringClass(input).map(handlerPackage(basePackage)).orElse(true);
		};
	}

	/**
	 * 处理包路径配置规则,支持多路径扫描匹配以逗号隔开
	 * java.util.Optional;
	 */
	private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
		return input -> {
			for (String strPackage : basePackage.split(";")) {
				boolean isMatch = input.getPackage().getName().startsWith(strPackage);
				if (isMatch) {
					return true;
				}
			}
			return false;
		};
	}

	/**
	 * java.util.function.Function;
	 */
	private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
		// swagger2.5.0版本没有input.declaringClass()这个方法 <swagger.version>2.8.0</swagger.version>
		return Optional.ofNullable(input.declaringClass());
	}

}